package com.situ.user.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.situ.user.bean.Order;

public interface OrderMapper {
	@Delete({ "delete from `order`", "where id = #{id,jdbcType=VARCHAR}" })
	int deleteByPrimaryKey(String id);

	@Insert({ "insert into `order`(id,user_id) values(#{id},#{userId})" })
	int insert(Order record);

	@Select({ "select", "id, user_id, address_id, time, express, status", "from `order`",
			"where id = #{id,jdbcType=VARCHAR}" })
	@Results(id = "order", value = { @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
			@Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
			@Result(column = "address_id", property = "addressId", jdbcType = JdbcType.INTEGER),
			@Result(column = "time", property = "time", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "express", property = "express", jdbcType = JdbcType.VARCHAR),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
			@Result(column = "id", property = "orderDetails", many = @Many(select = "com.situ.user.mapper.OrderDetailsMapper.selectByOrderId")),
			@Result(column = "address_id", property = "address", one = @One(select = "com.situ.user.mapper.AddressMapper.selectByPrimaryKey")) })
	Order selectByPrimaryKey(String id);

	@Select({ "select", "id, user_id, address_id, time, express, status", "from `order`" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
			@Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
			@Result(column = "address_id", property = "addressId", jdbcType = JdbcType.INTEGER),
			@Result(column = "time", property = "time", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "express", property = "express", jdbcType = JdbcType.VARCHAR),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER) })
	List<Order> selectAll();

	@Update({ "update `order`", "set user_id = #{userId,jdbcType=INTEGER},",
			"address_id = #{addressId,jdbcType=INTEGER},", "time = #{time,jdbcType=TIMESTAMP},",
			"express = #{express,jdbcType=VARCHAR},", "status = #{status,jdbcType=INTEGER}",
			"where id = #{id,jdbcType=VARCHAR}" })
	int updateByPrimaryKey(Order record);

	@Select("select * from `order` where user_id = #{userId} order by time desc")
	@ResultMap("order")
	List selectByUserId(Integer userId);

	@Select("select * from `order` where time < date_sub(now(),interval 30 minute) and status =0")
	@ResultMap("order")
	List<Order> selectInvalid();
}